package Dao.Implement;

import Dao.roomDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import pojo.room;

import java.math.BigInteger;
import java.util.List;

@Repository(value = "roomDaoImplement")
public class roomDaoImplement implements roomDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<room> queryRoomByCategory(String sports_id) {
        String sql="select * from room where sports_id=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<room>(room.class),sports_id);
    }

    @Override
    public room queryRoomById(Long room_id) {
        String sql="select * from room where room_id=?";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<room>(room.class),room_id);
        } catch (EmptyResultDataAccessException e) {
           return null;
        }
    }

    @Override
    public int updateCurrentNumber(int currentNumber ,Long room_id) {

        String sql="update room set current_number=? where room_id=?";
        return jdbcTemplate.update(sql,currentNumber,room_id);
    }

    @Override
    public int updateMatchValue(double MatchValue,Long room_id) {
        String sql="update room set pk_match=? where room_id=?";
        return jdbcTemplate.update(sql,MatchValue,room_id);
    }

    @Override
    public Object queryMaxRoomID() {
       String sql="select max(room_id) from room";
        try {
            return jdbcTemplate.queryForObject(sql,Integer.class);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}

